package com.example.muyonghui.mytimetable;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;

    private EditText name;

    private EditText code;

    private EditText school;

    private EditText classname;

    private EditText birth;

    private Button save;

    private String username;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        name = (EditText) findViewById(R.id.name);
        code = (EditText) findViewById(R.id.code);
        school = (EditText) findViewById(R.id.school);
        classname = (EditText) findViewById(R.id.classname);
        birth = (EditText) findViewById(R.id.birth);
        save = (Button) findViewById(R.id.user_info_save);

        dbHelper = new MyDatabaseHelper(this, "UserStore.db", null, 3);

        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        username = pref.getString("username", "");
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("user", null, "username like '"+username+"'", null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                // 遍历Cursor对象，取出数据并打印
                name.setText(cursor.getString(cursor. getColumnIndex("name")));
                code.setText(cursor.getString(cursor. getColumnIndex("code")));
                school.setText(cursor.getString(cursor. getColumnIndex("school")));
                classname.setText(cursor.getString(cursor. getColumnIndex("class")));
                birth.setText(cursor.getString(cursor. getColumnIndex("birth")));
            } while (cursor.moveToNext());
        }
        cursor.close();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                save();

                Toast.makeText(UserInfoActivity.this, "修改成功！",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(UserInfoActivity.this, MainActivity.class);
                UserInfoActivity.this.startActivity(intent);
                finish();
            }
        });

    }

    public void save() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("name", name.getText().toString());
        cv.put("code", code.getText().toString());
        cv.put("school", school.getText().toString());
        cv.put("class", classname.getText().toString());
        cv.put("birth", birth.getText().toString());
        db.update("user", cv, "username = ?", new String[]{username});
    }
}
